<?php
/**
 * Created by PhpStorm.
 * User: 何婷婷
 * Date: 2016/11/8
 * Time: 14:28
 */

namespace App\Model;

use App\Core\DB;
use App\core\Model;

class UserModel extends Model
{

    /**
     * array $Data  存储数据表user里面的字段
     * $keys=('id','name','password')
     *
     */
    public function __construct($id= null)
    {
        parent::__construct('user');
        //$this->getUserById($id);
    }

    // 判断用户是否存在
    public function isExistName($name)
    {
        if (empty($name))
            $name = $this->data['name'];
        if (empty($name))
            return false;
        $result = $this->db->setWhere(array('name' => $name))->select();
        if ($result) {
            return true;
        } else
            return false;
    }

    //判断用户电话是否存在
    public function isExistTel($tel)
    {
        //TODO:to do userData
        if (empty($tel))
            $tel = $this->data['tel'];//data['tel'] 数据库里面已经存在的数据
        if (empty($tel))
            return false;
        $result = $this->db->setWhere(array('tel' => $tel))->select();
        if ($result) {
            return true;
        } else
            return false;
    }

    //判断邮箱是否存在
    public function isExistEmail($email)
    {
        //TODO:to do userData
        if (empty($email))
            $email = $this->data['email'];
        if (empty($email))
            return false;
        $result = $this->db->setWhere(array('email' => $email))->select();
        if ($result) {
            return true;
        } else
            return false;
    }

    //获取user的id
    /*public function getUserById($id)
    {

        /*$result = $this->db->setWhere(array('id' => $id))->select();
        if ($result) {
            $this->data = array('id' => $result['id'],
                'name' => $result['name'],
                'password' => $result['password'],
                'tel' => $result['tel'],
                'email' => $result['email'],
                'realname' => $result['realname'],
                'count' => $result['count'],
                'qq' => $result['qq'],
                'time' => $result['time'],
                'ip' => $result['ip'],
            );
            var_dump($this->data);

        }
        return parent::getById($id);
    }*/

   /* //获取user的id
    public function getUserId($name) {
        return parent::getId($name);
    }*/

    //获取用户信息
    public function listUserInfo($name)
    {
        return parent::listTable($name);
    }

    //获取user表中字段
    public function __get($name)
    {
        return $this->data[$name];
    }

    //修改user表中的字段
    public function __set($name, $value)
    {
        return $this->data[$name] = $value;
    }

    //用户验证
    public function check($name = '', $password = '')
    {
        $result = $this->db->setWhere(array('name' => $name, 'password' => md5($password)))->select();
        if ($result)
            return true;
        else
            return false;
    }

    /*//显示用户数据
    public function userList($where)
    {
        $result = $this->db->setWhere($where)->select();
        if (empty($result)) {
            return false;
        }
        return $result;
    }*/
}